<template>
  <div class="mod-fii__fiiclasshourfile">
    <el-form :inline="true" :model="dataForm" @keyup.enter="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.file" placeholder="文件名称" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">{{ $t("query") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="exportHandle()">{{ $t("export") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('fii:fiiclasshourfile:save')" type="primary" @click="uploadCourseFile()">{{ $t("add") }}</el-button>
      </el-form-item>
      <el-form-item>
        <el-button v-if="hasPermission('fii:fiiclasshourfile:delete')" type="danger" @click="deleteHandle()">{{ $t("deleteBatch") }}</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
      <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
      <el-table-column prop="name" label="文件名称" header-align="center" align="center" width="350"></el-table-column>

      <el-table-column prop="type" label="附件类型" header-align="center" align="center" width="100">
        <template v-slot="scope">
          <el-tag v-if="scope.row.type === 1" size="small" type="">word</el-tag>
          <el-tag v-else-if="scope.row.type === 2" size="small" type="">video</el-tag>
          <el-tag v-else-if="scope.row.type === 3" size="small" type="">pdf</el-tag>
          <el-tag v-else size="small" type="">其他</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column prop="url" label="链接地址" header-align="center" align="center"> </el-table-column>-->
      <el-table-column prop="url" label="下载" header-align="center" align="center" width="100">
        <template v-slot="scope">
          <el-button size="small" type="primary" @click="downFile(scope.row.url, scope.row.name)">下载</el-button>
        </template>
      </el-table-column>
      <!--      <el-table-column prop="createAt" label="创建时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="creator" label="创建人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updateAt" label="修改时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="updator" label="修改人" header-align="center" align="center"></el-table-column>-->
      <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
        <template v-slot="scope">
          <el-button v-if="hasPermission('fii:fiiclasshourfile:delete')" type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t("delete") }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="page" :page-sizes="[10, 20, 50, 100]" :page-size="limit" :total="total" layout="total, sizes, prev, pager, next, jumper" @size-change="pageSizeChangeHandle" @current-change="pageCurrentChangeHandle"> </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>

  <el-dialog v-model="showDialogFlag" width="40%" :modal="false" :title="showDialogFlagTitle" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :inline="true" size="small" :model="dataForm">
      <el-form-item label="课时" prop="categoryId">
        <el-select v-model="classHourId" placeholder="分类" :rules="[{ required: true, message: '请选择分类', trigger: 'blur' }]">
          <el-option v-for="item in courseList" :key="item.id" :label="item.title" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上传文件" prop="pic">
        <el-upload v-loading="fileLoading" :action="url" drag :before-upload="beforeUploadHandle" :on-success="successHandle" class="text-center">
          <el-icon class="el-icon--upload" style="width: 200px"><upload-filled /></el-icon>
          <div class="el-upload__text" v-html="$t('upload.text')"></div>
        </el-upload>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button type="default" @click="showDialogFlag = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="closeDialog">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts">
import useView from "@/hooks/useView";
import { defineComponent, reactive, toRefs } from "vue";
import AddOrUpdate from "./fiiclasshourfile-add-or-update.vue";
import { IObject } from "@/types/interface";
import app from "@/constants/app";
import { getToken } from "@/utils/cache";
import baseService from "@/service/baseService";
export default defineComponent({
  components: {
    AddOrUpdate
  },
  props: { courseId: String },
  created: function () {
    this.dataForm.courseId = this.courseId as string;
    this.dataForm.eq_courseId = this.courseId as string;
    this.getDataList();
    //上传课件地址
    this.url = `${app.api}/fii/fiiclasshourfile/upload?&fileType=courseFile&token=${getToken()}` + "&courseId=" + this.courseId + "&classHourId=" + this.classHourId;
  },
  setup() {
    const state = reactive({
      fileLoading: false,
      getDataListURL: "/fii/fiiclasshourfile/page",
      getDataListIsPage: true,
      showDialogFlag: false,
      showDialogFlagTitle: "上传文件",
      exportURL: "/fii/fiiclasshourfile/export",
      deleteURL: "/fii/fiiclasshourfile",
      url: "",
      deleteIsBatch: true,
      fileList: [],
      dataForm: {
        id: null,
        file: "",
        courseId: "",
        eq_courseId: "",
        classHourId: null
      },
      courseList: [{ id: null, title: "" }],
      classHourId: null
    });
    return { ...useView(state), ...toRefs(state) };
  },
  watch: {},
  methods: {
    //上传文件
    uploadCourseFile() {
      this.getClassHourList();
      this.showDialogFlag = true;
    },
    // 上传之前
    beforeUploadHandle(file: IObject) {
      this.fileLoading = true;
      if (file.type !== "image/jpg" && file.type !== "image/jpeg" && file.type !== "image/png" && file.type !== "image/gif") {
        //  this.$message.error(this.$t("upload.tip", { format: "jpg、png、gif" }));
        // return false;
      }
    },
    // 上传成功
    successHandle(res: IObject, file: IObject, fileList: IObject[]) {
      this.$nextTick(() => {
        // 以服务的方式调用的 Loading 需要异步关闭
        this.fileLoading = false;
      });

      this.dataForm.id = res.data.file.id;
      this.dataForm.classHourId = this.classHourId;
      baseService.put("/fii/fiiclasshourfile", this.dataForm).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        } else {
          this.query();
        }
      });
    },
    closeDialog() {
      this.showDialogFlag = false;
    },
    downFile(url: string, name: string) {
      baseService.downloadOss(url, {}, name);
    },
    // 课时列表
    getClassHourList() {
      baseService.get("/fii/fiiclasshour/list", { courseId: this.dataForm.courseId }).then((res) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.courseList = res.data;
      });
    }
  }
});
</script>
